package com.yonyou.common.dialog;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcs.log.Hash;
import com.yonyou.component.CheckOutProcess;
import com.yonyou.component.MultiComboBox;
import com.yonyou.demo.CtmDemo;
import git4idea.GitCommit;
import git4idea.GitLocalBranch;
import git4idea.commands.Git;
import git4idea.history.GitHistoryUtils;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public class ReplaceDialogWrapper extends DialogWrapper {

    private List<VirtualFile> vFiles;
    private   Project project;
    private JPanel center = new JPanel();
    private JPanel south = new JPanel();

    private JLabel flag = new JLabel("原始文本：");

    private JTextArea flag_text= new JTextArea();
    private JLabel flag1 = new JLabel("最终文本：");

    private JTextArea flag_text1= new JTextArea();
    public static int text_h=35;
    JButton sureBtn = new JButton("确认");
    JButton cancleBtn = new JButton("取消");


    public ReplaceDialogWrapper(  Project project, List<VirtualFile> vFiles) {
        super(true);
        this.vFiles=vFiles;
        this.project=project;
        setTitle("智能替换"); //设置会话框标题
        init(); //触发一下init方法，否则swing样式将无法展示在会话框
    }

    @Override
    protected JComponent createNorthPanel() {
        return null;
    }


    @Override
    protected JComponent createSouthPanel() {
        sureBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        sureBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(sureBtn, BorderLayout.EAST);

        cancleBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        cancleBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(cancleBtn, BorderLayout.EAST);


        //按钮事件绑定
        sureBtn.addActionListener(e -> {
            try {
                final String text = flag_text.getText();
                final String text1 = flag_text1.getText();
                if(CollectionUtils.isEmpty(vFiles)){
                    com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("异常","未找到替换文本");
                    return;
                }
                if(StringUtils.isEmpty(text)){
                    com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("异常","原文本不可以为空");
                    return;
                }
                int count=0;
                for (VirtualFile virtualFile : vFiles) {
                    final boolean b = CtmDemo.replaceTextInFile(project, virtualFile, text, text1);
                    if(b){
                        count++;
                    }
                }
                final String format = String.format("替换完成：找到文件【%d】个，成功替换【%d】个", vFiles.size(), count);
                com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("提示",format);
            } catch (Exception ex) {
                this.close(DialogWrapper.CANCEL_EXIT_CODE);
                com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("异常提示",ex.getMessage());
            }
        });
        cancleBtn.addActionListener(e -> {
            this.close(DialogWrapper.CANCEL_EXIT_CODE);
        });

        return south;
    }

    @Override
    protected JComponent createCenterPanel() {

        final GridBagLayout gridLayout = new GridBagLayout();
        center.setLayout(gridLayout);

        GridBagConstraints g3 = new GridBagConstraints();
        g3.gridx=1;
        g3.gridy=1;
        g3.insets = new Insets(20,0,20,0);

        flag.setForeground(new Color(139, 181, 20)); //设置字体颜色
        flag.setHorizontalAlignment(SwingConstants.CENTER);
        flag.setVerticalAlignment(SwingConstants.TOP);
        center.add(flag,g3);

        GridBagConstraints g4 = new GridBagConstraints();
        g4.gridx=2;
        g4.gridy=1;
        g4.insets = new Insets(20,0,20,0);
        flag_text.setLineWrap(true);//设置换行
        flag_text.setPreferredSize(new Dimension(220,80));
        center.add(flag_text,g4);

        GridBagConstraints g31 = new GridBagConstraints();
        g31.gridx=1;
        g31.gridy=2;
        g31.insets = new Insets(20,0,20,0);

        flag1.setForeground(new Color(139, 181, 20)); //设置字体颜色
        flag1.setHorizontalAlignment(SwingConstants.CENTER);
        flag1.setVerticalAlignment(SwingConstants.TOP);
        center.add(flag1,g31);

        GridBagConstraints g41 = new GridBagConstraints();
        g41.gridx=2;
        g41.gridy=2;
        g41.insets = new Insets(20,0,20,0);
        flag_text1.setLineWrap(true);//设置换行
        flag_text1.setPreferredSize(new Dimension(220,80));
        center.add(flag_text1,g41);
        return center;
    }

}

